package com.igl.talk.android.client.ide.models;/*
 * @author Adam Wieczorek 
 *
 * 12/03/2014
 *
 * All rights reserved.
 */

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.igl.talk.android.client.R;
import com.igl.talk.android.client.ide.emoticons.EmoticonsBuilder;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class MessageAdapter extends ArrayAdapter<IdeMessageModel> {

    private List<IdeMessageModel> messagesList = new ArrayList<>();
    private Context context;

    public MessageAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
        this.context = context;
    }

    @Override
    public void add(IdeMessageModel object) {
        messagesList.add(object);
        super.add(object);
    }

    public int getCount() {
        return this.messagesList.size();
    }

    public IdeMessageModel getItem(int index) {
        return this.messagesList.get(index);
    }

    public View getView(int index, View view, ViewGroup parent) {
        View row = view;
        if (row == null) {
            LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.message, parent, false);
        }
        IdeMessageModel mesg = getItem(index);

        TextView date = (TextView) row.findViewById(R.id.messageDate);
        SimpleDateFormat dateFormatter = new SimpleDateFormat("dd-MM-yy HH:mm", Locale.getDefault());
        date.setText(dateFormatter.format(mesg.getDate()));

        TextView author = (TextView) row.findViewById(R.id.messageAuthor);
        String authorBundle = context.getResources().getString(R.string.message_author, mesg.getUserName());
        author.setText(authorBundle);

        TextView text = (TextView) row.findViewById(R.id.messageText);
        text.setText(EmoticonsBuilder.buildEmoticonsMessage(context, mesg.getMessage()));

        LinearLayout.LayoutParams textParams = (LinearLayout.LayoutParams)text.getLayoutParams();
        LinearLayout.LayoutParams authorParams = (LinearLayout.LayoutParams)author.getLayoutParams();
        if (mesg.isReceived()) {
            text.setBackgroundResource(R.drawable.left);
            textParams.gravity = authorParams.gravity = Gravity.LEFT;
            textParams.leftMargin = 0;
            textParams.rightMargin = 50;
        } else {
            text.setBackgroundResource(R.drawable.right);
            textParams.gravity = authorParams.gravity = Gravity.RIGHT;
            textParams.rightMargin = 0;
            textParams.leftMargin = 50;
        }
        text.setLayoutParams(textParams);
        author.setLayoutParams(authorParams);

        return row;
    }
}
